package com.cisc.business.order.service.impl;

import com.cisc.business.order.dao.OrderMapper;
import com.cisc.business.order.dto.OrderInfo;
import com.cisc.business.order.service.OrderService;
import com.cisc.business.order.service.ProductService;
import com.cisc.business.order.service.UserService;
import com.cisc.business.product.dto.ProductInfo;
import com.cisc.business.user.dto.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @description: XXX
 * @author: daizh
 * @time: 2021/6/20 14:41
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ProductService productService;

    @Resource
    private UserService userService;

    @Override
    public OrderInfo getOrderInfoById(Integer id) {
        return orderMapper.getOrderInfoById(id);
    }

    @Override
    public List<OrderInfo> getOrderInfoByUserId(Integer userId) {
        return orderMapper.getOrderInfoByUserId(userId);
    }

    @Override
    public List<OrderInfo> getOrderInfoByProductId(Integer productId) {
        return orderMapper.getOrderInfoByProductId(productId);
    }

    @Override
    public List<OrderInfo> getAllOrders() {
        return orderMapper.getAllOrders();
    }

    @Override
    public void saveOrder(OrderInfo orderInfo) {
        orderMapper.saveOrder(orderInfo);
    }

    @Override
    public void createOrder(Integer userId, Integer productId) {
        ProductInfo productInfo = productService.getProductByProductId(productId);
        if (Objects.isNull(productInfo)) {
            throw new RuntimeException("product:[" + productId + "]不存在");
        }
        UserInfo userInfo = userService.getUserById(userId);
        if (Objects.isNull(userInfo)) {
            throw new RuntimeException("user:[" + productId + "]不存在");
        }

        // 保存订单
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        orderInfo.setProductId(productId);
        orderInfo.setPrice(new BigDecimal(5999));
        orderInfo.setNum(3);
        orderInfo.setTotal(new BigDecimal(17997));
        orderMapper.saveOrder(orderInfo);
    }
}
